package com.study.r;

import io.swagger.annotations.ApiModelProperty;
import org.apache.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * @author study
 * @version 1.0
 * @date 2020/6/29 0:17
 */
public class Rmap<T> extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty(value = "返回数据")
    private T datas;


    public Rmap datas(T t) {
        this.setDatas(t);
        return this;
    }

    public Rmap() {
        put("code", 0);
        put("msg", "success");
    }

    public static Rmap error() {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常，请联系管理员");
    }

    public static Rmap error(String msg) {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    public static Rmap error(int code, String msg) {
        Rmap r = new Rmap();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static Rmap ok(String msg) {
        Rmap r = new Rmap();
        r.put("msg", msg);
        return r;
    }

    public static Rmap ok(Map<String, Object> map) {
        Rmap r = new Rmap();
        r.putAll(map);
        return r;
    }

    public static Rmap ok() {
        return new Rmap();
    }

    public Rmap put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public T getDatas() {
        return datas;
    }

    public void setDatas(T datas) {
        this.datas = datas;
    }

    @Override
    public String toString() {
        return "Rmap{" +
                "datas=" + datas +
                '}';
    }
}
